﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class Help : View
    {
        // Font list
        private SpriteFont headFont, longFont, backFont;

        // Text display padding
        private Vector2 padding;

        public Help()
            : base()
        {
            // Set default padding
            padding = new Vector2(50, 20);
        }

        public override void LoadContent(ContentManager content)
        {
            headFont = content.Load<SpriteFont>("Head");
            longFont = content.Load<SpriteFont>("Long");
            backFont = content.Load<SpriteFont>("Back");
        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.Enter)
            {
                // Go back to menu
                ViewManager.State = GameState.Menu;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            String longContent = String.Empty;
            String backContent = "Back to menu";
            String headContent = "Help";

            //longContent += "There is help information. How the game can be played, how to control paddles and";
            //longContent += "how winner is determined. Content of this text box should be wrapped.";
            //longContent += "The whole text specification is in the Help.cs file.";

            longContent += "The goal of PONG ULTIMATE is to be the first player to reach a set score limit, this can be changed in the settings. ";
            longContent += "Points are gained by getting the ball to leave the screen on the opposite side of your bat. ";
            longContent += "\n" + "\n" + "Player 1 controls the LEFT bat, using W to move upwards and S to move downwards. ";
            longContent += "Player 2 controls the RIGHT bat, using the UP-ARROW to move upwards and DOWN-ARROW to move downwards. ";
            longContent += "Beware that the game controls powerups that may revert your controls. ";
            longContent += "\n" + "There are also some powerups that reduces and increases both your bat's movement speed and size. ";
            longContent += "See the icons on the powerups to learn what they do. ";
            longContent += "\n" + "\n" + "To pause the game, use P. Music can be set on and muted with M.";

            // Get available viewport width
            Int32 availableWidth = (Int32)Math.Round(spriteBatch.GraphicsDevice.Viewport.Width - 2 * padding.X);

            // Set minimal width
            availableWidth = Math.Max(100, availableWidth);

            // Modify the text to fit into viewport
            longContent = StringHelper.wrap(longFont, longContent, availableWidth);

            // Display text positions
            Vector2 headPosition = new Vector2(spriteBatch.GraphicsDevice.Viewport.Width / 2, 0);
            Vector2 longPosition = new Vector2(padding.X, padding.Y);
            Vector2 backPosition = new Vector2(spriteBatch.GraphicsDevice.Viewport.Width / 2, spriteBatch.GraphicsDevice.Viewport.Height);

            // Find right side of the left string
            Vector2 headOrigin = headFont.MeasureString(headContent);
            Vector2 backOrigin = backFont.MeasureString(backContent);

            // Modify position according to size
            headPosition = new Vector2(headPosition.X - headOrigin.X / 2, headPosition.Y + padding.Y);
            longPosition = new Vector2(longPosition.X, longPosition.Y + headOrigin.Y + padding.Y);
            backPosition = new Vector2(backPosition.X - backOrigin.X / 2, backPosition.Y - padding.Y - backOrigin.Y);

            // Draw current score to the ground
            spriteBatch.DrawString(headFont, headContent, headPosition, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(longFont, longContent, longPosition, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(backFont, backContent, backPosition, Color.Red, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
        }
    }
}
